package com.kobol.webclient.client;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtplatform.mvp.client.DelayedBindRegistry;
import com.kobol.webclient.client.gin.WebclientGinjector;

public class Webclient implements EntryPoint {

    interface GlobalResources extends ClientBundle {
	@NotStrict
	@Source("webclient.css")
	CssResource css();
    }

    private static WebclientConstants constants;

    public final WebclientGinjector ginjector = GWT.create(WebclientGinjector.class);

    private long startTimeMillis;

    public void onModuleLoad() {

	// Defer all application initialisation code to onModuleLoad2() so that
	// the
	// UncaughtExceptionHandler can catch any unexpected exceptions.
	Log.setUncaughtExceptionHandler();

	DeferredCommand.addCommand(new Command() {
	    public void execute() {
		onModuleLoad2();
	    }
	});
    }

    private void onModuleLoad2() {
	try {

	    if (!Log.isLoggingEnabled()) {
		Window.alert("Logging is disabled.");
	    }

	    if (Log.isDebugEnabled()) {
		startTimeMillis = System.currentTimeMillis();
	    }

	    // Log.trace("This is a 'TRACE' test message");
	    Log.debug("onModuleLoad2()");
	    // Log.info("This is a 'INFO' test message");
	    // Log.warn("This is a 'WARN' test message");
	    // Log.error("This is a 'ERROR' test message");
	    // Log.fatal("This is a 'FATAL' test message");

	    // inject global styles
	    GWT.<GlobalResources> create(GlobalResources.class).css().ensureInjected();

	    // load constants
	    constants = (WebclientConstants) GWT.create(WebclientConstants.class);

	    /*
	     * Dictionary dictionary = Dictionary.getDictionary("Pages");
	     * 
	     * String page = dictionary.get("page");
	     * 
	     * if (page.equals("MainPage")) {
	     * 
	     * }
	     */

	    // this is required by gwt-platform proxy's generator
	    DelayedBindRegistry.bind(ginjector);

	    ginjector.getPlaceManager().revealCurrentPlace();

	    // hide the animated 'loading.gif'
	    RootPanel.get("loading").setVisible(false);

	    if (Log.isDebugEnabled()) {
		long endTimeMillis = System.currentTimeMillis();
		float durationSeconds = (endTimeMillis - startTimeMillis) / 1000F;
		Log.debug("Duration: " + durationSeconds + " seconds");
	    }

	} catch (Exception e) {
	    Log.error("e: " + e);
	    e.printStackTrace();

	    Window.alert(e.getMessage());
	}
    }

    public static WebclientConstants getConstants() {
	return constants;
    }
}